package cn.edu.scau.cmi.chenyuanying.factoryMethod;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextArea;

public class FactoryMethodController implements Initializable {
	@FXML private Button showButton;
	@FXML private ChoiceBox<String> choiceBox;
	@FXML private TextArea showTextArea;
	private String nowMeat; 
	private final String[] chooseType = {"Please choose", "Pork", "Beef", "Chicken"};
	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		ObservableList<String> list = FXCollections.observableArrayList(chooseType); 
		nowMeat = chooseType[0];
		choiceBox.setItems(list);
		choiceBox.getSelectionModel().selectFirst();
		choiceBox.getSelectionModel().selectedIndexProperty().addListener(
				new ChangeListener<Number>() {
					@Override
					public void changed(ObservableValue<? extends Number> observable, Number oldValue,
							Number newValue) {
						nowMeat = chooseType[newValue.intValue()];
					}
				});
	}
	
	public void showMeat(ActionEvent e) {
		MeatFactory factory = null;; 
		if(nowMeat.compareTo(chooseType[1]) == 0) {
			factory = new PorkMeatFactory();
		} else if(nowMeat.compareTo(chooseType[2]) == 0) {
			factory = new BeefMeatFactory();
		} else if(nowMeat.compareTo(chooseType[3]) == 0) {
			factory = new ChickenMeatFactory();
		}
		showTextArea.setText(factory == null ? "" : "from:" + factory.getFactoryInfo() + "\n" + factory.getMeatObj().toString());
	}
}
